//
//  PhotoBrowseViewController.m
//  LimitFree
//
//  Created by lijinghua on 16/3/2.
//  Copyright © 2016年 lijinghua. All rights reserved.
//

#import "PhotoBrowseViewController.h"
#import "PhotoModel.h"

@interface PhotoBrowseViewController ()<UICollectionViewDataSource>

@property(nonatomic)UICollectionView *collectionView;

@end

@implementation PhotoBrowseViewController

- (id)init{
    if (self = [super init]) {
        self.navigationStyle = LFNavigationBackStyle;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.collectionView];
    
    //让cell滚动到指定的位置
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentSelectIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}

- (UICollectionView*)collectionView{
    if (_collectionView == nil) {
        //使用布局对象来初始化UICollectionView
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.itemSize = CGSizeMake(kScreenWidth, kScreenHeight-64);
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64) collectionViewLayout:flowLayout];
        _collectionView.pagingEnabled = YES;
        _collectionView.dataSource = self;
        //_collectionView.delegate   = self;
        
        //注册要使用的cell
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
    }
    return _collectionView;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.photoArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    //方法1:
    //[[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    //方法2:
    UIImageView *imageView = [cell.contentView viewWithTag:9999];
    if (imageView == nil) {
        imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64)];
        imageView.tag = 9999;
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [cell.contentView addSubview:imageView];
    }
    
    //在cell 上放置一个imageView用于显示图片
//    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64)];
//    imageView.tag = 9999;
//    imageView.contentMode = UIViewContentModeScaleAspectFit;
//    [cell.contentView addSubview:imageView];
    
    PhotoModel *model = self.photoArray[indexPath.row];
    [imageView sd_setImageWithURL:[NSURL URLWithString:model.originalUrl] placeholderImage:nil];
    
    return cell;
}



@end
